package cn.achuan.spring.springcloud.demo;

import cn.achuan.spring.springcloud.demo.product.Product;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductEndpoint {
    private static final Logger logger = LoggerFactory.getLogger(ProductEndpoint.class);

    @Autowired
    private ProductService productService;

    // 获取商品列表
    @RequestMapping(method = RequestMethod.GET)
    public List<Product> list() {
        return productService.findAll();
    }

    // 获取指定商品的详情
    @RequestMapping(value = "/{itemCode}", method = RequestMethod.GET)
    public Product detail(@PathVariable String itemCode) {
        logger.debug(".................. invoke success ..................");
        return productService.findOne(itemCode);
    }

    // 该端点仅仅是用来测试消息发送，并不包含任何业务逻辑处理
    @RequestMapping(value = "/{itemCode}", method = RequestMethod.POST)
    public Product save(@PathVariable String itemCode) {
        Product product = this.productService.findOne(itemCode);
        if (product != null) {
            this.productService.save(product);
        }
        return product;
    }
}